#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <glib.h>
#include <string.h>
#include "ssb.h"


int main(int argc, char ** argv){
	int fp_date , fp_lo ;
	struct ddate tmp_date, *p;
	struct lineorder tmp_lo;
	GHashTable * date_hash = NULL;
	unsigned long long sum = 0;

	if(argc != 3){
		printf("error:date lo\n");
		exit(-1);
	}

	fp_date = open(argv[1],O_RDONLY);
	fp_lo = open(argv[2],O_RDONLY);

	date_hash = g_hash_table_new(NULL,NULL);

	while(read(fp_date,(void *)&tmp_date,sizeof(struct ddate))>0){
		if (strcmp(tmp_date.yearmonth,"199401")==0){
			p = (struct ddate *) malloc(sizeof(struct ddate));
			memcpy(p,&tmp_date,sizeof(struct ddate));
			g_hash_table_insert(date_hash,GINT_TO_POINTER(p->datekey),p);
		}
	}

	while(read(fp_lo,(void*)&tmp_lo,sizeof(struct lineorder))>0){
		p = (struct ddate *) g_hash_table_lookup(date_hash,GINT_TO_POINTER(tmp_lo.orderdate));
		if(p == NULL)
			continue;

		if(tmp_lo.discount>=4 && tmp_lo.discount<=6 && tmp_lo.quantity <=36 && tmp_lo.quantity>=25){
			sum += tmp_lo.extendedprice * tmp_lo.discount;
		}
	}
	
	printf("%llu\n",sum);
	close(fp_date);
	close(fp_lo);

	return  0;
}
